package bb.lanxing.model.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import androidx.annotation.IntRange;

import com.orm.dsl.Column;
import bb.lanxing.model.bean.Place;
import bb.lanxing.model.map.GeoBounds;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.LitePal;

public class MapPOI extends LitePalTableBase implements Parcelable {
    public static final Parcelable.Creator<MapPOI> CREATOR = new Parcelable.Creator<MapPOI>() {
        public MapPOI createFromParcel(Parcel in2) {
            return new MapPOI(in2);
        }

        public MapPOI[] newArray(int size) {
            return new MapPOI[size];
        }
    };
    public static final int TYPE_CAR_SHOP = 1;
    public static final int TYPE_CAR_SHOP_AUTH = 2;
    public static final int TYPE_NORMAL = 0;

    @Column(name = "EXT")
    private String ext;
    @Column(name = "LATITUDE")
    private double latitude;
    @Column(name = "LEVEL")
    private int level;
    @Column(name = "LONGITUDE")
    private double longitude;
    @Column(name = "POI_ID")
    private long poiID;
    @Column(name = "TIMESTAMP")
    private long timestamp;
    @IntRange(from = 0)
    @Column(name = "TYPE")
    private int type;

    public static int getPOILevel(int type) {
        return (type == 1 || type == 2) ? 10 : 8;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    protected MapPOI(Parcel in2) {
        this.type = in2.readInt();
        this.poiID = in2.readLong();
        this.latitude = in2.readDouble();
        this.longitude = in2.readDouble();
        this.timestamp = in2.readLong();
        this.ext = in2.readString();
        this.level = in2.readInt();
    }

    // input param i2 means enable (order and limit)
    public static List<MapPOI> queryPoi(GeoBounds geoBounds, int i, int i2) {
        String query = "select * from mapPOI where latitude < ? and latitude > ? and longitude < ? and longitude > ? and level <= ?";
        if (i2 > 0) {
            query = query + " order by RANDOM() limit ?";
        }
        GeoBounds type = geoBounds.toType(0);

        SQLiteDatabase db = LitePal.getDatabase();

        Cursor rawQuery = db.rawQuery(query, i2 > 0 ? new String[]{String.valueOf(type.north), String.valueOf(type.south), String.valueOf(type.east), String.valueOf(type.west), String.valueOf(i), String.valueOf(i2)} : new String[]{String.valueOf(type.north), String.valueOf(type.south), String.valueOf(type.east), String.valueOf(type.west), String.valueOf(i)});
        ArrayList arrayList = new ArrayList(rawQuery.getCount());
        while (rawQuery.moveToNext()) {
            int typeIndex = rawQuery.getColumnIndex("TYPE");
            if (typeIndex < 0) continue;
            int poiIdIndex = rawQuery.getColumnIndex("POI_ID");
            if (poiIdIndex < 0) continue;
            int latitudeIndex = rawQuery.getColumnIndex("LATITUDE");
            if (latitudeIndex < 0) continue;
            int longitudeIndex = rawQuery.getColumnIndex("LONGITUDE");
            if (longitudeIndex < 0) continue;
            int timestampIndex = rawQuery.getColumnIndex("TIMESTAMP");
            if (timestampIndex < 0) continue;
            int extIndex = rawQuery.getColumnIndex("EXT");
            if (extIndex < 0) continue;
            int levelIndex = rawQuery.getColumnIndex("LEVEL");
            if (levelIndex < 0) continue;

            arrayList.add(new MapPOI(rawQuery.getInt(typeIndex)
                    , rawQuery.getLong(poiIdIndex)
                    , rawQuery.getDouble(latitudeIndex)
                    , rawQuery.getDouble(longitudeIndex)
                    , rawQuery.getLong(timestampIndex)
                    , rawQuery.getString(extIndex)
                    , rawQuery.getInt(levelIndex)));


        }

        rawQuery.close();
        return arrayList;
    }

    public static List<MapPOI> queryByType(int i) {
        String type = String.valueOf(i);
        List<MapPOI> l = LitePal.where("type=?", type).find(MapPOI.class);
        return l;
    }

    public MapPOI() {
    }

    public MapPOI(int type, long poiID, double latitude, double longitude, long timestamp, String ext, int level) {
        this.type = type;
        this.poiID = poiID;
        this.latitude = latitude;
        this.longitude = longitude;
        this.timestamp = timestamp;
        this.ext = ext;
        this.level = level;
    }

    public Parcelable getExtObject() {
        if (TextUtils.isEmpty(this.ext)) {
            return null;
        }
        int i = this.type;
        if (i == 1 || i == 2) {
            try {
                Place place = new Place(new JSONObject(this.ext));
                place.setMapPoi(this);
                return place;
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public int getType() {
        return this.type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public long getPoiID() {
        return this.poiID;
    }

    public void setPoiID(long poiID) {
        this.poiID = poiID;
    }

    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getExt() {
        return this.ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public int getLevel() {
        return this.level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MapPOI mapPOI = (MapPOI) o;
        if (this.type == mapPOI.type && this.poiID == mapPOI.poiID && Double.compare(mapPOI.latitude, this.latitude) == 0 && Double.compare(mapPOI.longitude, this.longitude) == 0 && this.timestamp == mapPOI.timestamp && this.level == mapPOI.level) {
            String str = this.ext;
            String str2 = mapPOI.ext;
            return str != null ? str.equals(str2) : str2 == null;
        }
        return false;
    }

    public int hashCode() {
        long j = this.poiID;
        long doubleToLongBits = Double.doubleToLongBits(this.latitude);
        long doubleToLongBits2 = Double.doubleToLongBits(this.longitude);
        long j2 = this.timestamp;
        int i = ((((((((this.type * 31) + ((int) (j ^ (j >>> 32)))) * 31) + ((int) (doubleToLongBits ^ (doubleToLongBits >>> 32)))) * 31) + ((int) (doubleToLongBits2 ^ (doubleToLongBits2 >>> 32)))) * 31) + ((int) (j2 ^ (j2 >>> 32)))) * 31;
        String str = this.ext;
        return ((i + (str != null ? str.hashCode() : 0)) * 31) + this.level;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.type);
        dest.writeLong(this.poiID);
        dest.writeDouble(this.latitude);
        dest.writeDouble(this.longitude);
        dest.writeLong(this.timestamp);
        dest.writeString(this.ext);
        dest.writeInt(this.level);
    }
}